﻿using System.ComponentModel;

namespace Zhp.Workflow.Models
{
    public interface IBaseWorkflow
    {
        /// <summary>
        /// 提交时间
        /// </summary>
        public DateTime? SubmitTime { get; set; }
        public long? SubmitUserId { get; set; }
        public string SubmitNickName { get; set; }
        /// <summary>
        /// 流程名称
        /// </summary>
        public string WorkflowName { get; set; }
        /// <summary>
        /// 最新流程实例ID
        /// </summary>
        public string WorkflowInstanceId { get; set; }
        /// <summary>
        /// 审批状态
        /// </summary>
        public ApproveStatusEnum ApproveStatus { get; set; }

        /// <summary>
        /// 流程状态
        /// </summary>
        public WorkflowStatusEnum WorkflowStatus { get; set; }

        /// <summary>
        /// 被审批个数,可以根据是否等于0判断是否可以取消流程
        /// </summary>
        public int? ApprovedCount { get; set; }
    }

    /// <summary>
    /// 扩展方法用于更新流程状态和审批状态
    /// </summary>
    public static class IBaseWorkflowExtensions
    {
        /// <summary>
        /// 提交
        /// </summary>
        public static void Submit(this IBaseWorkflow baseWorkflow, string workflowInstanceId, string workflowName, long submitUserId, string submitNickName)
        {
            baseWorkflow.SubmitTime = DateTime.Now;
            baseWorkflow.SubmitUserId = submitUserId;
            baseWorkflow.SubmitNickName = submitNickName;
            baseWorkflow.WorkflowInstanceId = workflowInstanceId;
            baseWorkflow.WorkflowName = workflowName;
            baseWorkflow.ApproveStatus = ApproveStatusEnum.Pending;
            baseWorkflow.WorkflowStatus = WorkflowStatusEnum.InProgress;
            baseWorkflow.ApprovedCount = 0;
        }

        /// <summary>
        /// 流程结束同意
        /// </summary>
        public static void FinishAgree(this IBaseWorkflow baseWorkflow)
        {
            baseWorkflow.ApprovedCount++;
            baseWorkflow.ApproveStatus = ApproveStatusEnum.Approved;
            baseWorkflow.WorkflowStatus = WorkflowStatusEnum.Finished;
        }

        /// <summary>
        /// 流程结束拒绝
        /// </summary>
        public static void FinishRefuse(this IBaseWorkflow baseWorkflow)
        {
            baseWorkflow.ApprovedCount++;
            baseWorkflow.ApproveStatus = ApproveStatusEnum.Rejected;
            baseWorkflow.WorkflowStatus = WorkflowStatusEnum.Finished;
        }

        /// <summary>
        /// 结束
        /// </summary>
        public static void Finish(this IBaseWorkflow baseWorkflow)
        {
            baseWorkflow.WorkflowStatus = WorkflowStatusEnum.Finished;
        }

        /// <summary>
        /// 取消
        /// </summary>
        public static void Cancel(this IBaseWorkflow baseWorkflow)
        {
            baseWorkflow.ApprovedCount = null;
            baseWorkflow.ApproveStatus = ApproveStatusEnum.NotSubmit;
            baseWorkflow.WorkflowStatus = WorkflowStatusEnum.Cancelled;
        }

        /// <summary>
        /// 流程中审批
        /// </summary>
        public static void Approve(this IBaseWorkflow baseWorkflow)
        {
            baseWorkflow.ApprovedCount++;
        }

        /// <summary>
        /// 将审批动作转换成审批状态
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public static ApproveStatusEnum ConvertToApproveStatus(this ApproveActionEnum action)
        {
            return action switch
            {
                ApproveActionEnum.Submit => ApproveStatusEnum.Pending,
                ApproveActionEnum.Agree => ApproveStatusEnum.Approved,
                ApproveActionEnum.Refuse => ApproveStatusEnum.Rejected,
                _ => ApproveStatusEnum.NotSubmit
            };
        }
    }


    /// <summary>
    /// 流程状态
    /// </summary>
    public enum WorkflowStatusEnum
    {
        /// <summary>
        /// 未开始
        /// </summary>
        [Description("未开始")]
        NotStarted = 0,
        /// <summary>
        /// 流程中
        /// </summary>
        [Description("流程中")]
        InProgress = 1,
        /// <summary>
        /// 流程结束
        /// </summary>
        [Description("流程结束")]
        Finished = 2,
        /// <summary>
        /// 取消
        /// </summary>
        [Description("取消")]
        Cancelled = 3
    }

    /// <summary>
    /// 审批状态
    /// </summary>
    public enum ApproveStatusEnum
    {
        /// <summary>
        /// 未提交
        /// </summary>
        [Description("未提交")]
        NotSubmit = 0,
        /// <summary>
        /// 审批中
        /// </summary>
        [Description("审批中")]
        Pending = 1,
        /// <summary>
        /// 通过
        /// </summary>
        [Description("通过")]
        Approved = 2,
        /// <summary>
        /// 拒绝
        /// </summary>
        [Description("拒绝")]
        Rejected = 3
    }

    /// <summary>
    /// 审批动作
    /// </summary>
    public enum ApproveActionEnum
    {
        /// <summary>
        /// 提交
        /// </summary>
        [Description("提交")]
        Submit,
        /// <summary>
        /// 同意
        /// </summary>
        [Description("同意")]
        Agree,
        /// <summary>
        /// 拒绝
        /// </summary>
        [Description("拒绝")]
        Refuse
    }

    /// <summary>
    /// 审批角色类型
    /// </summary>
    public enum ApproverRoleEnum
    {
        /// <summary>
        /// 用户
        /// </summary>
        [Description("用户")]
        User,
        /// <summary>
        /// 角色
        /// </summary>
        [Description("角色")]
        Role,
    }
}
